<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="110" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="lhmProductionStatistics"
      :columns="columns"
      :dataSource="dataSource"
      :isExportExcel="true"
      exportFileName="生产统计.xlsx"
      :isMemoryPagination="true"
      :isSelectColumn="isSelectColumn"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChangeHandle"
      :onSyncTableData="tableAllData"
    >
      <template slot="controls" slot-scope="props">
        <!-- <el-button size="small" type="primary" @click="saveHandle">打印</el-button> -->
      </template>
    </FilterTable>
    <BasePrint ref="print" :data="printList" template="productionStatistics" />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getProductionStatistics } from '@/api/repair/queryStatistics/queryAccounts';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getServiceAdciserData, searchServiceAdvisor } from '@/api/repair/queryStatistics/queryAccounts';
// 维修类别
import { getfindAllRepairType } from '@/api/repair/business/customer/history';
// 车型
import { getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
// 查询服务顾问
import { getUserListSelect } from '@/api/report/repair/repairreport';
// 二手车置换
export default {
  name: 'LHMProductionStatistics',
  components: {},
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      dataSource: [],
      isSelectColumn: false,
      columns: this.createTableCheckColumns(),
      printList: {},
      serviceAdvisorDict: []
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.querySeries();
    this.queryModel();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '日期范围',
          fieldName: 'beginDate|endDate',
          placeholder: '选择日期',
          initialValue: [
            moment(new Date())
              .startOf('month')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD')
          ],
          rules: [{ required: true, message: '请选日期范围', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '类别',
          fieldName: 'repairTypeCode',
          placeholder: '请选择...',
          clearable: true,
          filterable: true,
          itemList: []
          // unitRender: () => {
          //   return <el-button slot="append" icon="el-icon-search" onClick={this.openManager6}></el-button>;
          // }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'model',
          placeholder: '请选择...',
          clearable: true,
          filterable: true,
          itemList: []
          // unitRender: () => {
          //   return <el-button slot="append" icon="el-icon-search" onClick={this.openManager7}></el-button>;
          // }
        }
      ];
    },
    createTableCheckColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '类别',
          dataIndex: 'REPAIR_TYPE_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '厂牌',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '车次',
          dataIndex: 'CC',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '目前在修',
          dataIndex: 'MQZX',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '已审车次',
          dataIndex: 'YSCC',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '结清车次',
          dataIndex: 'JQCC',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '欠账车次',
          dataIndex: 'QZCC',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '索赔返修',
          dataIndex: 'SPNF',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '内修车次',
          dataIndex: 'NXSL',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '未结车次',
          dataIndex: 'WJCC',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        },
        {
          title: '当天完工车次',
          dataIndex: 'DTWJCC',
          sorter: true,
          filter: true,
          summation: true,
          filterType: 'number'
        }
      ];
    },
    // 维修类型
    async querySeries() {
      const res = await getfindAllRepairType();
      let list = res.data.map(x => ({ text: x.REPAIR_TYPE_NAME, value: x.REPAIR_TYPE_CODE + '' }));
      this.topFilterList.find(item => item.fieldName === 'repairTypeCode').itemList = list; // 维修类型
    },
    // 车型
    async queryModel() {
      const res = await getModel();
      this.topFilterList.find(x => x.fieldName === 'model').itemList = res.data.map(x => ({ text: x.MODEL_NAME, value: x.MODEL_ID + '' }));
    },
    tableAllData(val) {},
    selectChangeHandle(val) {
      this.selectedRow = val;
    },
    async changeHandle(val) {
      this.dataSource = [];
      this.BaseTable.START_LOADING();
      // console.log('val', val);
      this.printList.beginDate = val.beginDate.slice(0, 10);
      this.printList.endDate = val.endDate.slice(0, 10);
      if (val.endDate) {
        val.beginDate = val.beginDate.slice(0, 10) + ' 00:00:00';
        val.endDate = val.endDate.slice(0, 10) + ' 23:59:59';
      }
      try {
        let res = await getProductionStatistics(val);
        if (res.resultCode == 200) {
          this.dataSource = res.data;
        }
      } catch (error) {
      } finally {
        this.BaseTable.STOP_LOADING();
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    saveHandle() {
      /* let printData = []; // 打印数组
      let serviceAdvisorList = []; // 存在的服务顾问
      let dataSource = _.cloneDeep(this.dataSource); // 打印数据
      // console.log('dataSource', dataSource);
      // console.log('serviceAdvisorDict', this.serviceAdvisorDict);
      for (const item1 of dataSource) {
        let key = this.serviceAdvisorDict.find(x => x.value == item1.SERVICE_ADVISOR);
        item1.SERVICE_ADVISOR = key ? key.text : item1.SERVICE_ADVISOR; // 维修顾问翻译
        if (serviceAdvisorList.indexOf(item1.SERVICE_ADVISOR) == -1) {
          serviceAdvisorList.push(item1.SERVICE_ADVISOR);
        }
      }
      // console.log('serviceAdvisorList', serviceAdvisorList);
      for (const item2 of serviceAdvisorList) {
        let obj1 = {
          // 一级对象
          total: 0,
          data: []
        };
        let data1 = []; // 按维修顾问分组数据
        for (const item3 of dataSource) {
          if (item2 == item3.SERVICE_ADVISOR) {
            data1.push(item3);
          }
        }
        obj1.total = data1.length; // 该维修顾问拥有数据条数
        let repairType = []; // 每个维修顾问所涉及的维修类型
        for (const item4 of data1) {
          if (repairType.indexOf(item4.REPAIR_TYPE_NAME) == -1) {
            repairType.push(item4.REPAIR_TYPE_NAME);
          }
        }
        // console.log('repairType', repairType, data1);
        for (const item5 of repairType) {
          let obj2 = {
            // 二级对象
            total: 0,
            data: []
          };
          for (const item6 of data1) {
            if (item5 == item6.REPAIR_TYPE_NAME) {
              obj2.data.push(item6);
            }
          }
          obj2.total = obj2.data.length; // 该维修顾问的该种维修类型数据条数
          obj1.data.push(obj2); // 将二级对象放进一级对象里
        }
        printData.push(obj1); // 将一级对象放进打印数据里
      }
      this.printList.data = printData;
      console.log(this.printList, 'printList'); */
      this.$refs.print.EXCUTE_PRINT();
    }
  }
};
</script>
<style lang="less" scoped></style>
